// mainPackage/pages/productAnswer/productAnswer.js
const app = getApp()

import util from "../../../utils/words";
import fUtil from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    questionList: [],
    productId: '',
    flag: false
  },


  getList() {
    var url = app.globalData.url + 'scProduct/listDiscussion',
      that = this;
    wx.showLoading({
      title: '',
      icon: ''
    })
    app.wxRequest(url, {
        productId: that.data.productId
        // productId: 14
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data;
          res.sort((a, b) => {
            return b.isTop - a.isTop
          })
          var arr = res.filter(item => {
            return item.discussionParentId == 0
          })
          arr.forEach(item => {
            item.list = []
            res.forEach(ele => {
              if (ele.discussionParentId == item.discussionId) {
                item.list.push(ele)
              }
            })
          })
          that.setData({
            questionList: arr,
            flag: arr.length == 0
          })
          console.log(arr);
          wx.hideLoading()
          wx.stopPullDownRefresh()
        }
      },
      err => {
        console.log(err);
      })
  },

  answerHandle(e) {
    var flag = e.currentTarget.dataset.flag;
    this.setData({
      flag: flag == 1
    })
  },


  submitHandle() {
    var url = app.globalData.url + 'ScDiscussion/addDiscussion',
      that = this,
      info = that.data.info;

    console.log(info);
    if (info.discussionContent == '') {
      wx.showToast({
        title: '请输入您的问题',
        icon: 'none'
      })
      return
    }
    if (info.discussionContent.length > 40) {
      wx.showToast({
        title: '问题不得超过40个字',
        icon: 'none'
      })
      return
    }

    if (util.filter_method(info.discussionContent).split("/")[1] == 1) {
      wx.showToast({
        title: '当前评论涉及到敏感词汇,请重新编辑',
        icon: 'none'
      })
      return
    }

    wx.showLoading({
      title: '',
      icon: ''
    })

    app.wxRequestJson(url, info, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.showToast({
            title: '发布成功',
            icon: 'none'
          })
          that.getList()
          that.setData({
            flag: false
          })

          wx.hideLoading()
        }
      },
      err => {
        console.log(err);
      })



  },
  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(this.data.info);
    var info = {
      companyId: app.globalData.companyId,
      discussionContent: '',
      discussionParentId: 0,
      discussionType: 0, // 0 问 1 答
      isTop: 0,
      productId: Number(options.id),
      // productId: 14,
      status: 1,
      userId: app.globalData.userId,
    }
    info.createTime = fUtil.formatTime1(new Date)

    this.setData({
      productId: Number(options.id),
      info
    })
    console.log(this.data.info);

    // this.submitHandle()
  },

  toPage(e) {
    var obj = e.currentTarget.dataset.obj;
    wx.navigateTo({
      url: '/mainPackage/pages/answerDetail/answerDetail?obj=' + JSON.stringify(obj),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})